<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/6
 * Time: 上午10:49
 */
namespace app\admin\controller;
use app\admin\model\AuditingModel;
use think\Db;

class Auditing extends Base
{

    /**
     * [index 文章列表]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index(){

        $key = input('key');
        $map = [];
        if($key&&$key!==""){
            $map['name'] = ['like',"%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 10;// 获取总条数
        $count = Db::name('auditing')->where('whetherown','=','0')->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $auditing = new AuditingModel();
        $lists = $auditing->getAuditingByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }



    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_auditing()
    {
        $auditing = new AuditingModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $auditing->updateAuditing($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $cate = Db::query('select * from think_category ');
        $this->assign('cate',$cate);
        $this->assign('auditing',$auditing->getOneAuditing($id));
        return $this->fetch();
    }



    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_auditing()
    {
        $id = input('param.id');
        $cate = new AuditingModel();
        $flag = $cate->delAuditing($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }


    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function auditing_shows()
    {
        $id=input('param.id');
        $shows = Db::name('auditing')->where(array('id'=>$id))->value('shows');//判断当前状态情况
        if($shows==1)
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }

    /** 审核通过列表 */
    public function adopt(){

        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['name'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('auditing')->where('whetherown','=','2')->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $auditing = new AuditingModel();
        $lists = $auditing->getAdoptByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }

    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_adopt()
    {
        $adopt = new AuditingModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $adopt->updateAdopt($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $cate = Db::query('select * from think_category ');
        $this->assign('cate',$cate);
        $this->assign('auditing',$adopt->getOneAdopt($id));
        return $this->fetch();
    }



    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_adopt()
    {
        $id = input('param.id');
        $cate = new AdoptModel();
        $flag = $cate->delAdopt($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }


    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function adopt_shows()
    {
        $id=input('param.id');
        $shows = Db::name('auditing')->where(array('id'=>$id))->value('shows');//判断当前状态情况
        if($shows==1)
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }





}